package cn.drew.org.controller;

import cn.drew.basic.utils.AjaxResult;
import cn.drew.basic.utils.PageList;
import cn.drew.org.domain.Employee;
import cn.drew.org.query.EmployeeQuery;
import cn.drew.org.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emp")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @GetMapping
    public List<Employee> findALl(){
        List<Employee> list = employeeService.findAll();
        return list;
    }

    @GetMapping("/{id}")
    public Employee findById(@PathVariable("id") Long id){
        Employee employee = employeeService.findById(id);
        return employee;
    }

    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Employee employee){

        if(employee.getId() != null){
            employeeService.update(employee);
        }else{
            employeeService.add(employee);
        }

        return AjaxResult.success();
    }

    @DeleteMapping("/{id}")
    public AjaxResult Delete(@PathVariable("id") Long id){
        employeeService.delete(id);
        System.out.println(id);
        return AjaxResult.success();
    }

//    分页查询
    @PostMapping
    public PageList<Employee> queryByPage(@RequestBody EmployeeQuery employeeQuery){
        PageList<Employee> pageList = employeeService.queryByPage(employeeQuery);
        System.out.println(pageList);
        return pageList;
    }

    //批量删除
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        employeeService.patchDelete(ids);
        return AjaxResult.success();
    }

    //通过departmentid查找员工
    @GetMapping("/findByDeptid/{deptid}")
    public List<Employee> findByDeptid(@PathVariable("deptid") Long deptid){
        return employeeService.findByDeptid(deptid);
    }

    
}
